<?php
/**
 * 好友管理
 *
 */
class attention_model_data extends Model
{
	const PAGE_SIZE=20;
	
	public function __construct()
	{
		parent::__construct();
		$this->table 		 = 'cartoon_friends';			//好友关系表
		$this->request_table = 'cartoon_friends_request';		//待确认请求列表
		$this->remark_table	 = 'cartoon_friends_remark';		//好友备注表
		$this->attention	=	'cartoon_attention';
	}
	
	/**
	 * 判断是否为好友 (废弃方法)
	 *
	 * @param int $host_id
	 * @param int $uid
	 * @return bool
	 */
	public function has($host_id, $uid)
	{
		$sql = "select * from ".$this->table." where host_id=&host_id and uid=&uid";
		$params = array('host_id'=>$host_id, 'uid'=>$uid);
		return $this->db->fetchRow($sql, $params) ? true : false;
	}
	
	public function chkAttention($uid,$host_id)
	{
		$sql = "select id from ".$this->attention."  where uid=$uid and c_uid=$host_id";
		return $this->db->fetchField($sql,'id')? true:false;
	}
	/**
	 * 获取关注的列表
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $page
	 * @param unknown_type $pageSize
	 * @return unknown
	 */
	public function getAttList($uid,$page=1,$pageSize = 15){
		if(!$uid) return false;
		$sql = "SELECT c_uid AS uid FROM $this->attention WHERE uid = $uid";
		
		return $this->db->selectPage($sql,array(),$page,$pageSize);
	}
	
	/**
	 * 添加好友	(废弃方法)
	 *
	 * @param array $arguments
	 * @return bool
	 */
	public function save($arguments)
	{
		$uid 	= (int) $arguments['uid'];
		$hostId = (int) $arguments['host_id'];
		if (!$uid || !$hostId) return false;
		return $this->db->query("INSERT IGNORE INTO {$this->table} (`uid`, `host_id`, `create_time`) VALUES ('{$uid}', '$hostId', '{$_SERVER['REQUEST_TIME']}')");
	}
	
	/**
	 * 查询分页结果
	 *
	 * @param int $host_id - 主人id
	 * @param int $page
	 * @param int $pagesize
	 * @return []
	 */
	public function selectByPage($host_id, $page, $pagesize){
		$sql = "select * from ".$this->table." where host_id=&host_id";
		$params = array('host_id'=>$host_id);
		return $this->db->selectPage($sql, $params, $page, $pagesize);
	}
	/**
	 * 查询列表
	 *
	 * @param int $host_id
	 * @param int $n
	 * @return []
	 */
	public function getList($host_id, $n=0){
		$sql = "select * from ".$this->table." where host_id=&host_id ".($n==0)?'':"limit $n";
		$params = array('host_id'=>$host_id);
		return $this->db->fetchAll($sql, $params);
	}
	
	public function add_attention($uid,$c_uid){
		$args = array('uid'=>$uid,'c_uid' => $c_uid,'addtime'=>CURRENT_TIME);
		return $this->db->insert('cartoon_attention',$args);
	}
	
	/**
	 * 删除
	 *
	 * @param int $uid - 被删除的uid
	 * @param int $host_id - 主人id
	 * @return boolean
	 */
	public function remove($uid, $host_id){
		$sql = "delete from ".$this->table." where uid=&uid and host_id=&host_id";
		$params = array(
			'uid'		=>	$uid,
			'host_id'	=>	$host_id
		);
		$flag = $this->db->query($sql, $params);
		return $flag;
	}
	
	public function remove_att($uid,$host_id){
		if(!($uid && $host_id)) return false;
		return $this->db->query("DELETE FROM $this->attention WHERE uid = $uid AND c_uid = $host_id");
	}
	
	/**
	 * 是否存在在关注列表里
	 *
	 * @param int $uid
	 * @param int $host_id
	 * @return boolean
	 */
	public function inList($uid, $host_id){
		$sql = "select id from ".$this->table." where uid=&uid and host_id=&host_id";
		$params = array(
			'uid'		=>	$uid,
			'host_id'	=>	$host_id
		);
		$row = $this->db->fetchRow($sql, $params);
		return ($row)?true:false;
	}
	
	public function selectAll($host_id){
		$sql = "select uid, username from ".$this->table." where host_id=&host_id";
		$params = array('host_id'=>$host_id);
		return $this->db->fetchAll($sql, $params);
	}
	
	
}